<?php

namespace App\Http\Controllers\Admin\SuperAdmin;

use App\Services\Admin\SuperAdmin\MenuService;
use Exception;
use Illuminate\Http\JsonResponse;

class MenuController extends BaseSuperAdminController
{
    private MenuService $menuService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->menuService = $this->loadService('Admin\SuperAdmin\MenuService');
    }

    /**
     * 获取菜单列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getMenu(): JsonResponse
    {
        $result = $this->menuService->getMenu();
        return outputJsonResult($result);
    }

    /**
     * 获取用户动态路由
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getMenuTree(): JsonResponse
    {
        $result = $this->menuService->getMenuTree();
        return outputJsonResult($result);
    }

    /**
     * 获取菜单列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getMenuList(): JsonResponse
    {
        $result = $this->menuService->getMenuList();
        return outputJsonResult($result);
    }

    /**
     * 根据id获取菜单
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getMenuById(): JsonResponse
    {
        $rule = [
            'id' => 'required|int|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $result = $this->menuService->getMenuById($this->params);
        return outputJsonResult($result);
    }

    /**
     * 获取指定角色menu
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getMenusByAuthorityId(): JsonResponse
    {
        $rule = [
            'authorityId' => 'required|int|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $result = $this->menuService->getMenusByAuthorityId($this->params);
        return outputJsonResult($result);
    }

    /**
     * 更新菜单
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function updateMenu(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'authorityId' => 'required|integer|min:1',
            'component' => 'required|string|min:1',
            'name' => 'required|string|min:1',
            'parentId' => 'integer',
            'path' => 'required|string|min:1',
            'sort' => 'integer',
            'meta.closeTab' => 'integer',
            'meta.defaultMenu' => 'integer',
            'meta.icon' => 'string',
            'meta.keepAlive' => 'integer',
            'meta.title' => 'required|string',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->menuService->updateMenu($this->params);

        return outputJsonResult();
    }

    /**
     * 添加菜单
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function addMenu(): JsonResponse
    {
        $rule = [
            'component' => 'required|string|min:1',
            'name' => 'required|string|min:1',
            'path' => 'required|string|min:1',
            'meta.title' => 'required|string',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->menuService->addMenu($this->params);

        return outputJsonResult();
    }

    /**
     * 删除菜单
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function deleteMenu(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->menuService->deleteMenu($this->params);

        return outputJsonResult();
    }

    /**
     * 添加角色菜单
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function addMenuAuthority(): JsonResponse
    {
        $rule = [
            'authorityId' => 'required|integer|min:1',
            'menus' => 'array',
            'menus.*.id' => 'integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonResult();
        }

        $this->menuService->addMenuAuthority($this->params);
        return outputJsonResult();
    }
}
